package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.myapplication.bean.ShakeValueBean;
import com.example.myapplication.bean.WifiDataBean;
import com.example.myapplication.socket.ServerSocketStart;
import com.example.myapplication.socket.UdpReceiveThread;
import com.example.myapplication.socket.UdpSendThread;
import com.example.myapplication.tools.Commons;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class DeviceDataActivity extends AppCompatActivity {

    private TextView localhostIPTv;
    private TextView localhostPortTv;
    private TextView deviceIdTv;
    private TextView deviceIPTv;
    private Button startWifiServerBtn;
    //    private Button refreshDeviceBtn;
    private Handler mHandler;
    private TextView deviceInfoHRRTv;
    private TextView deviceInfoHHRTv;
    private TextView deviceInfoHMeanRRTv;
    private TextView deviceInfoHMeanHRTv;
    private TextView deviceInfoHMaxHRTv;
    private TextView deviceInfoHMinHRTv;
    private TextView deviceInfoHSpO2Tv;
    private TextView deviceInfoHPITv;
    private TextView deviceInfoSGradTv;
    private TextView deviceInfoSFluTv;
    private TextView deviceInfoSPPTv;
    private TextView deviceInfoSMAXTv;
    private TextView deviceInfoSMINTv;
    private TextView deviceInfoSAVGTv;
    private TextView deviceInfoBRCTv;
    private TextView deviceInfoBRRTv;
    private TextView deviceInfoBRQTv;
    private TextView deviceInfoBRPTv;
    private TextView deviceInfoBAVGPRETv;
    private Button localReceiveDataBtn;
    private ServerSocketStart serverSocketStart;
    private TextView deviceInfoShakeValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_data);
        initView();
        initEvent();
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x126) {
                    String result = (String) msg.obj;
                    Gson gson = new Gson();
                    /**
                     * ！！！！！坑
                     * 清洗数据 板子返回的数据不规则 将下划线字段转为驼峰命名法
                     */
                    String jsonNew = Commons.jsonKeyEdit(result);
                    /**
                     * 坑解决完毕
                     */
                    if (result.contains("ShakeValue")) {
                        String newResult = Commons.formatJson(result);
                        String jsonNew1 = Commons.jsonKeyEdit(newResult);
                        Log.d("test", "handleMessage: " + jsonNew1);
                        ShakeValueBean shakeValueBean = gson.fromJson(jsonNew1,ShakeValueBean.class);
                        deviceInfoShakeValue.setText(shakeValueBean.getShakevalue());
                    } else {
                        Log.d("test", "handleMessage: " + jsonNew);
                        WifiDataBean wifiDataBean = gson.fromJson(jsonNew, WifiDataBean.class);
                        deviceIdTv.setText(wifiDataBean.getDeviceid());
                        deviceInfoHRRTv.setText(wifiDataBean.getHRr());
                        deviceInfoHHRTv.setText(wifiDataBean.getHHr());
                        deviceInfoHMeanRRTv.setText(wifiDataBean.getHMeanrr());
                        deviceInfoHMeanHRTv.setText(wifiDataBean.getHMeanhr());
                        deviceInfoHMaxHRTv.setText(wifiDataBean.getHMaxhr());
                        deviceInfoHMinHRTv.setText(wifiDataBean.getHMinhr());
                        deviceInfoHSpO2Tv.setText(wifiDataBean.getHSpo2());
                        deviceInfoHPITv.setText(wifiDataBean.getHPi());
                        deviceInfoSGradTv.setText(wifiDataBean.getSGrad());
                        deviceInfoSFluTv.setText(wifiDataBean.getSFlu());
                        deviceInfoSPPTv.setText(wifiDataBean.getSPp());
                        deviceInfoSMAXTv.setText(wifiDataBean.getSMax());
                        deviceInfoSMINTv.setText(wifiDataBean.getSMin());
                        deviceInfoSAVGTv.setText(wifiDataBean.getSAvg());
                        deviceInfoBRCTv.setText(wifiDataBean.getBRc());
                        deviceInfoBRRTv.setText(wifiDataBean.getBRr());
                        deviceInfoBRQTv.setText(wifiDataBean.getBRq());
                        deviceInfoBRPTv.setText(wifiDataBean.getBRp());
                        deviceInfoBAVGPRETv.setText(wifiDataBean.getBAvgpre());
                    }

                }
                if (msg.what == 0x127) {
                    String result = (String) msg.obj;
                    String[] deviceInfo = result.split(":");
                    deviceIPTv.setText(deviceInfo[0].substring(deviceInfo[0].indexOf("/") + 1) + "");

                }
            }
        };
    }

    public void initView() {
        localhostIPTv = findViewById(R.id.localhost_ip_tv);
        localhostPortTv = findViewById(R.id.localhost_port_tv);
        deviceIdTv = findViewById(R.id.device_id_tv);
        deviceIPTv = findViewById(R.id.device_IP_tv);
        localReceiveDataBtn = findViewById(R.id.local_receive_btn);
        startWifiServerBtn = findViewById(R.id.start_wifi_server_btn);
//        refreshDeviceBtn = findViewById(R.id.refresh_device_btn);
        localhostIPTv.setText(Commons.getIpAddressString() + "");
        //设备信息控件
        deviceInfoShakeValue = findViewById(R.id.device_info_ShakeValue_tv);
        deviceInfoHRRTv = findViewById(R.id.device_info_HRR_tv);
        deviceInfoHHRTv = findViewById(R.id.device_info_HHR_tv);
        deviceInfoHMeanRRTv = findViewById(R.id.device_info_HMeanRR_tv);
        deviceInfoHMeanHRTv = findViewById(R.id.device_info_HMeanHR_tv);
        deviceInfoHMaxHRTv = findViewById(R.id.device_info_HMaxHR_tv);
        deviceInfoHMinHRTv = findViewById(R.id.device_info_HMinHR_tv);
        deviceInfoHSpO2Tv = findViewById(R.id.device_info_HSpO2_tv);
        deviceInfoHPITv = findViewById(R.id.device_info_HPI_tv);
        deviceInfoSGradTv = findViewById(R.id.device_info_SGrad_tv);
        deviceInfoSFluTv = findViewById(R.id.device_info_SFlu_tv);
        deviceInfoSPPTv = findViewById(R.id.device_info_SPP_tv);
        deviceInfoSMAXTv = findViewById(R.id.device_info_SMAX_tv);
        deviceInfoSMINTv = findViewById(R.id.device_info_SMIN_tv);
        deviceInfoSAVGTv = findViewById(R.id.device_info_SAVG_tv);
        deviceInfoBRCTv = findViewById(R.id.device_info_BRC_tv);
        deviceInfoBRRTv = findViewById(R.id.device_info_BRR_tv);
        deviceInfoBRQTv = findViewById(R.id.device_info_BRQ_tv);
        deviceInfoBRPTv = findViewById(R.id.device_info_BRP_tv);
        deviceInfoBAVGPRETv = findViewById(R.id.device_info_BAVGPRE_tv);


    }

    public void initData() {

    }

    public void initEvent() {
        startWifiServerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                serverSocketStart = new ServerSocketStart(mHandler, 60002);
                serverSocketStart.start();
            }
        });
        //刷新设备 进行优化 暂不开启使用
//        refreshDeviceBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });
        localReceiveDataBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DeviceDataActivity.this, LocalReceiveActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}